package services

import (
	"fmt"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/mysql/db_main"
	"longmen/server/pkg/db/redis/cache"

	"github.com/jinzhu/copier"
	"longmen/server/pkg/common/api_models"
)

func (s *Service) CreateTestAnchorIncomeModels(uid int) ([]*api_models.CreateTestAnchorIncomeModels, error) {
	fName := "CreateTestAnchorIncomeModels"
	users, err := db_main.GetUsersParam(map[string]interface{}{}, "vip_date desc", 1, 50)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	ids := make([]int, 0)
	for i := range users {
		ids = append(ids, users[i].Id)
	}
	anchorMap, err := s.GetAnchors(0, ids, "", 0, 0)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	data := make([]*api_models.CreateTestAnchorIncomeModels, 0)
	income := 20200618
	for i := range users {
		anchorIncome := &api_models.CreateTestAnchorIncomeModels{}
		anchorIncome.Anchorid = users[i].Id
		anchorIncome.Income = income
		err = copier.Copy(&anchorIncome.Anchor, anchorMap[users[i].Id])
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}
		income -= 200
		data = append(data, anchorIncome)
	}
	if uid > 0 {
		attentids := cache.GetUserAttentAnchorIds(uid)
		for i := range attentids {
			for j := range data {
				if data[j].Anchor.Id == attentids[i] {
					data[j].Anchor.Isattent = 1
				}
			}
		}
	}
	return data, nil
}

/*
func (this *Service) GetAnchorRankList(r *api_models.GetAnchorRankListReq) ([]*api_models.CreateTestAnchorIncomeModels, error) {
	date := make([]string, 2)
	switch r.Type {
	case 0:
		date = []string{} //today
	case 1:
		date = []string{} //week
	case 2:
		date = []string{} //month
	default:
		date = []string{}
	}
	fmt.Println(date)
	return nil, nil

	   if ($date){
	       $list = AnchorIncomeModel::with('anchor')->whereTime('date', $date)
	           ->orderRaw("sum(income) desc")->field(['id','anchorid',"sum(income) income"])->group('anchorid')->limit(0,50)->select();
	   }else{
	       $list = AnchorIncomeModel::with('anchor')->orderRaw("sum(income) desc")->field(['id','anchorid',"sum(income) income"])->group('anchorid')->limit(0,50)->select();
	   }

	   if ($uid = Request::post("uid")){
	       $attentids = getUserAttentAnchorIds($uid);
	       foreach ($attentids as $attentid){
	           foreach ($list as $index=>$item){
	               if ($item->anchor->id == $attentid){
	                   $item->anchor->isattent = 1;
	               }
	           }
	       }
	   }
}
*/

func (s *Service) CreateTestUserConsumeModels(uid int) ([]*api_models.CreateTestUserConsumeModels, error) {
	fName := "CreateTestUserConsumeModels"
	userMap, err := s.GetAnchors(0, []int{}, "vip_date desc", 1, 50)
	if err != nil {
		return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	data := make([]*api_models.CreateTestUserConsumeModels, 0)
	consume := 20200618
	for i := range userMap {
		userConsume := &api_models.CreateTestUserConsumeModels{}
		userConsume.Uid = i
		userConsume.Consume = consume
		err = copier.Copy(&userConsume.User, userMap[i])
		if err != nil {
			return nil, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
		}
		consume -= 200
		data = append(data, userConsume)
	}
	if uid > 0 {
		attentids := cache.GetUserAttentAnchorIds(uid)
		for i := range attentids {
			for j := range data {
				if data[j].User.Id == attentids[i] {
					data[j].User.Isattent = 1
				}
			}
		}
	}
	return data, nil
}

/*
func (this *Service) GetUserRankList(r *api_models.GetUserRankListReq) ([]*api_models.CreateTestUserConsumeModels, error) {
	date := make([]string, 2)
	switch r.Type {
	case 0:
		date = []string{} //today
	case 1:
		date = []string{} //week
	case 2:
		date = []string{} //month
	default:
		date = []string{}
	}
	fmt.Println(date)
	return nil, nil
	   if ($date){
	       $list = UserConsumeModel::with('user')->whereTime('date', $date)
	           ->orderRaw("sum(consume) desc")->field(['id','uid',"sum(consume) consume"])->group('uid')->limit(0,50)->select();
	   }else{
	       $list = UserConsumeModel::with('user')->orderRaw("sum(consume) desc")->field(['id','uid',"sum(consume) consume"])->group('uid')->limit(0,50)->select();
	   }

	   if ($uid = Request::post("uid")){
	       $attentids = getUserAttentAnchorIds($uid);
	       foreach ($attentids as $attentid){
	           foreach ($list as $index=>$item){
	               if ($item->user->id == $attentid){
	                   $item->user->isattent = 1;
	               }
	           }
	       }
	   }
}
*/
